#include "httpserver.hpp"
#include <memory>
using namespace server;
using namespace std;
void Usage(std::string proc)
{
    std::cerr << "Usage:\n\t" << proc << " port\r\n\r\n";
}
std::string suffixToDesc(const std::string suffix)
{
    std::string ct = "Content-Type: ";
    if (suffix == ".html")
    {
        ct += "text/html";
    }
    else if (suffix == ".jpg")
    {
        ct += "application/x-jpg";
    }
    ct += "\r\n";
    return ct;
}
bool Get(const HttpRequest &req, HttpResponse &resp)
{
    cout << "------------------------http start----------------------" << endl;
    cout << req.inbuffer << std::endl;
    std::cout << "method : " << req.method << std::endl;
    std::cout << "url : " << req.url << std::endl;
    std::cout << "httpversion : " << req.httpversion << std::endl;
    std::cout << "path : " << req.path << std::endl;
    std::cout << "suffix : " << req.suffix << std::endl;
    std::cout << "size : " << req.size << "字节" << std::endl;
    cout << "------------------------http end------------------------" << endl;
    std::string respline = "HTTP/1.1 200 OK\r\n";
    // std::string respline = "HTTP/1.1 307 Temporary Redirect\r\n";
    // 我们要正确的给客户端返回资源类型,我们自己怎么知道呢,因为所有的资源都有后缀
    // std::string respheader = "Content-Type: text/html\r\n";//.html的
    std::string respheader = suffixToDesc(req.suffix);
    if (req.size > 0)
    {
        // postman
        //  respheader += "Content-Length: ";
        //  respheader += std::to_string(req.size);
        //  respheader += "\r\n";
    }
    // 往后: 每次http请求, 都会自动携带曾经设置的所有的cookie, 帮服务器进行鉴权行为----http会话保持
    respheader += "Set-Cookie: name=1234567abcdefg; Max-Age=120\r\n";
    // respheader += "Location: https://www.qq.com/\r\n";
    std::string respblank = "\r\n";
    // std::string body = "<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>for test</title><h1>hello world</h1></head><body><p>焦点访谈丨从改革先锋到发展样板 读懂深圳高质量发展答卷扩大清洁用能，让绿色能源点亮万家灯火何以中国｜“津”彩跃动，彰显“何以中国”深厚底气</p></body></html>";
    std::string body;

    // if (!Util::readFile(req.path, &body))
    // {
    //     Util::readFile(html_404, &body); // 假设一定能成功
    // }

    body.resize(req.size + 1);
    if (!Util::readFile(req.path, (char *)body.c_str(), req.size))
    {
        Util::readFile(html_404, (char *)body.c_str(), req.size);
    }

    // postman
    respheader += "Content-Length: ";
    respheader += std::to_string(body.size());
    respheader += "\r\n";

    resp.outbuffer += respline;
    resp.outbuffer += respheader;
    resp.outbuffer += respblank;
    cout << "------------------------http response start----------------------" << endl;
    std::cout << resp.outbuffer << std::endl;
    cout << "------------------------http response end------------------------" << endl;
    resp.outbuffer += body;
    return true;
}
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t port = atoi(argv[1]);
    unique_ptr<httpServer> httpsvr(new httpServer(Get, port));
    // httpsvr->registerCb("/", Get);
    httpsvr->initServer();
    httpsvr->start();
    return 0;
}
